#!/usr/bin/python3.7
# -*- coding: utf-8 -*-
# @Time    : 2021/3/9 6:00 下午
# @Author  : 白辉东
# @Email   : xyxk-huidong.bai@ximalaya.com
# @File    : matchLog.py
# @Software: PyCharm

from public.utils import logUtils
from config import projectPath
from public.common.result import Result

wifi_error_tag = '/oem/OrionOSVoice/default/error_network_error_check_it.mp3'
wak_up_tag = 'WAKEUP  小雅小雅  detected'
asr_result_tag = '---query:'
resp_tts_tag = '---tts:'
nlu_domain = '---domain:'
nlu_intent = '---intent:'
tts_start_tag = 'PLAY_TTS_STARTED'
payload_tag = 'AudioPlayer procAdditionalInfo payload :'


def match_log_mideamini(query):
    print('match_log_mideamini 开始')
    log = logUtils.open_log(projectPath.LINUX_CASH_LOG_DIR)
    result = Result(query)

    # 是否被唤醒成功
    result.is_wakeup_success = bool(logUtils.get_line_by_key(log, wak_up_tag))
    print('result.is_wakeup_success:%s' % str(result.is_wakeup_success))
    # 返回的asr识别结果
    result.asr_result = logUtils.get_line_by_key(log, asr_result_tag).split(asr_result_tag)[1].strip()
    print('result.asr_result:%s' % result.asr_result)
    # 返回的tts
    result.speaker_tts = logUtils.get_line_by_key(log, resp_tts_tag).split(resp_tts_tag)[1].strip()
    print('result.speaker_tts:%s' % result.speaker_tts)
    # TODO 查看是否为兜底TTS
    result.is_doudi_tts = False
    # 查看是否有网络异常提示
    result.is_wifi_disconnect = bool(logUtils.get_line_by_key(log, wifi_error_tag))
    print('result.is_wifi_disconnect:%s' % str(result.is_wifi_disconnect))
    # 返回的domain
    result.nlu_domain = logUtils.get_line_by_key(log, nlu_domain).split(nlu_domain)[1].strip()
    print('result.nlu_domain:%s' % result.nlu_domain)
    # 返回的intent
    result.nlu_intent = logUtils.get_line_by_key(log, nlu_intent).split(nlu_intent)[1].strip()
    print('result.nlu_intent:%s' % result.nlu_intent)
    # 若存在tts，则判断是否有tts开始标志
    if len(query.tts_start_tag):
        result.tts_start_exist = bool(logUtils.get_line_by_key(log, tts_start_tag))
        print('result.tts_start_exist:%s' + str(result.tts_start_exist))
    # 若存在play_url，则判断是否有payload标志
    if len(query.play_url_tag):
        result.play_url_exist = bool(logUtils.get_line_by_key(log, payload_tag))
        print('result.play_url_exist:%s' % str(result.play_url_exist))
    # 若存在音箱状态动作，则判断是否存在该日志
    if len(query.speaker_action):
        result.speaker_action = logUtils.get_line_by_key(log, query.speaker_action)
        print('result.speaker_action:%s' % result.speaker_action)
    print('match_log_mideamini 结束')
    return result
